﻿namespace fpsrpg
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;

    using xWinFormsLib;

    using WinForms = System.Windows.Forms;

    public partial class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Fields

       private SpriteBatch mBatch;
       private Texture2D mHealthBar;
       private Texture2D mXpBar;
       private Texture2D mXpBar_Inner;
       private Texture2D Weapon_Bar;
       private Texture2D Weapon_Hud;
       private SpriteFont smallGuiFont;

        #endregion Fields

        #region Methods

        /// <summary>
        /// Draws the entire ui
        /// </summary>
        /// <param name="gametime">GameTime as passed in by XNA</param>
       protected void DrawUI(GameTime gametime)
       {
           mBatch.Begin();

           DrawHealthBar(gametime);
           DrawWeaponBar(gametime);
           DrawXpBar(gametime);
           DrawPlayerFrame(gametime);

           mBatch.End();
       }

        /// <summary>
        /// Draws the healthbar of the player.
        /// </summary>
        /// <param name="gametime">GameTime as passed in by XNA</param>
        protected void DrawHealthBar(GameTime gametime)
        {
            

            //Draw the negative space for the health bar
            mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2, this.Window.ClientBounds.Height - 120, mHealthBar.Width, 60), new Rectangle(0, 45, mHealthBar.Width, 60), Color.Gray);

            //Draw the current health level based on the current Health
            mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2, this.Window.ClientBounds.Height - 120, (int)(mHealthBar.Width * ((double)Player.Health / 100)), 60), new Rectangle(0, 45, mHealthBar.Width, 60), Color.Red);

            //Draw the box around the health bar
            mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2, this.Window.ClientBounds.Height - 120, mHealthBar.Width, 60), new Rectangle(0, 0, mHealthBar.Width, 60), Color.White);
        }

        /// <summary>
        /// Draws the weapon ui.
        /// </summary>
        /// <param name="gametime">GameTime as passed in by XNA</param>
        protected void DrawWeaponBar(GameTime gametime)
        {
            mBatch.Draw(Weapon_Bar, new Rectangle(this.Window.ClientBounds.Width - Weapon_Bar.Width -10, this.Window.ClientBounds.Height - Weapon_Bar.Height -10, Weapon_Bar.Width, Weapon_Bar.Height), null, Color.White);

            if (Weapon_Hud != null)
            {
                mBatch.Draw(Weapon_Hud, new Rectangle(this.Window.ClientBounds.Width - Weapon_Hud.Width - 10, this.Window.ClientBounds.Height - Weapon_Hud.Height - 10, Weapon_Hud.Width, Weapon_Hud.Height), null, Color.White);
            }

            mBatch.DrawString(UVfont, Player.GetAmmoString(), new Vector2(this.Window.ClientBounds.Width - Weapon_Bar.Width / 2, this.Window.ClientBounds.Height - Weapon_Bar.Height / 2 - 10), Color.White);

        }

        /// <summary>
        /// Draws the XP Bar
        /// </summary>
        /// <param name="gameTime">GameTime as passed in by XNA</param>
        protected void DrawXpBar(GameTime gameTime)
        {
            
            //Inner Bar
            mBatch.Draw(mXpBar_Inner, new Rectangle(this.Window.ClientBounds.Width / 2 - mXpBar_Inner.Width / 2, this.Window.ClientBounds.Height -40, (int)(mXpBar_Inner.Width * Player.GetXpFrac()), 16),null, Color.White);
            
            //Outer frame
            mBatch.Draw(mXpBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mXpBar.Width / 2, this.Window.ClientBounds.Height -40, mXpBar.Width, 16), null, Color.Black);
            mBatch.DrawString(UVfont, string.Format("{0}%", Player.GetXpFrac() * 100), new Vector2((int)(Window.ClientBounds.Width / 2) -5, this.Window.ClientBounds.Height - 35), Color.White);
        }


        protected void DrawPlayerFrame(GameTime gameTime)
        {
            mBatch.Draw(Weapon_Bar, new Rectangle(10, this.Window.ClientBounds.Height - Weapon_Bar.Height - 10, Weapon_Bar.Width, Weapon_Bar.Height), null, Color.White);
            mBatch.DrawString(smallGuiFont, Player.Name, new Vector2(20, Window.ClientBounds.Height - Weapon_Bar.Height - 5), Color.White);
            mBatch.DrawString(smallGuiFont,"Level: " + Convert.ToString(Player.Level), new Vector2(20, Window.ClientBounds.Height - Weapon_Bar.Height + 12), Color.White);
            mBatch.DrawString(smallGuiFont, "XP: " + Player.GetXpString(), new Vector2(20, Window.ClientBounds.Height - Weapon_Bar.Height + 30), Color.White);
        }

        /// <summary>
        /// Loads the textures needed for the health bar
        /// </summary>
        protected void LoadhealthBar()
        {
            mBatch = new SpriteBatch(this.graphics.GraphicsDevice);

            mHealthBar = Content.Load<Texture2D>("HealthBar");
            smallGuiFont = Content.Load<SpriteFont>("SmallGuiFont");
        }

        /// <summary>
        /// Loads the textures for the weapon bar
        /// </summary>
        protected void LoadWeaponBar()
        {
            mBatch = new SpriteBatch(this.graphics.GraphicsDevice);

            Weapon_Bar = Content.Load<Texture2D>("weapon_background");
        }

        /// <summary>
        /// Loads the textures need for the weapon bar
        /// </summary>
        protected void LoadXpBar()
        {
            mBatch = new SpriteBatch(this.graphics.GraphicsDevice);

            mXpBar = Content.Load<Texture2D>("crappy_bar");
            mXpBar_Inner = Content.Load<Texture2D>("crappy_xp_fill_bar");
        }

        /// <summary>
        /// Updates the weapon bar to point to the same as what the player is using
        /// </summary>
        protected void UpdateWeaponBar()
        {
            Weapon_Hud = Player.GetWeaponHudTexture();
        }

        #region GUI XWindows Events

        private void Button1_OnPress(object obj, EventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine("Button Pressed!");
            //((Listbox)formCollection["form1"]["listbox1"]).Add(formCollection["form1"].Controls["textbox1"].Text);
            if (Properties.Settings.Default.Customize_hat != Player.hats.Customize_hat)
            {
                Properties.Settings.Default.Customize_hat += 1;
                Player.hats.Customize_hat_switch(Properties.Settings.Default.Customize_hat, this.Content);
                label_customize_hat_title.Text = Player.hats.Customize_hat_title;
            }
            else
            {
                Properties.Settings.Default.Customize_hat = 1;
                Player.hats.Customize_hat_switch(Properties.Settings.Default.Customize_hat, this.Content);
                label_customize_hat_title.Text = Player.hats.Customize_hat_title;
            }
        }

        private void Button2_OnPress(object obj, EventArgs e)
        {
            if (Properties.Settings.Default.Customize_hat != 1)
            {
                Properties.Settings.Default.Customize_hat -= 1;
                Player.hats.Customize_hat_switch(Properties.Settings.Default.Customize_hat, this.Content);
                label_customize_hat_title.Text = Player.hats.Customize_hat_title;
            }
            else
            {
                Properties.Settings.Default.Customize_hat = 4;
                Player.hats.Customize_hat_switch(Properties.Settings.Default.Customize_hat, this.Content);
                label_customize_hat_title.Text = Player.hats.Customize_hat_title;
            }
        }

        private void Button3_OnPress(object obj, EventArgs e)
        {
            if (Properties.Settings.Default.Customize_body != Player.number_of_bodies)
            {
                Properties.Settings.Default.Customize_body += 1;
                Player.Customize_body_switch(Properties.Settings.Default.Customize_body, this.Content);
                label_customize_body_title.Text = Player.Customize_body_title;
            }
            else
            {
                Properties.Settings.Default.Customize_body = 1;
                Player.Customize_body_switch(Properties.Settings.Default.Customize_body, this.Content);
                label_customize_body_title.Text = Player.Customize_body_title;
            }
        }

        private void Button4_OnPress(object obj, EventArgs e)
        {
            if (Properties.Settings.Default.Customize_body != 1)
            {
                Properties.Settings.Default.Customize_body -= 1;
                Player.Customize_body_switch(Properties.Settings.Default.Customize_body, this.Content);
                label_customize_body_title.Text = Player.Customize_body_title;
            }
            else
            {
                Properties.Settings.Default.Customize_body = 2;
                Player.Customize_body_switch(Properties.Settings.Default.Customize_body, this.Content);
                label_customize_body_title.Text = Player.Customize_body_title;
            }
        }

        private void NameChangeTextBox_update(object obj, EventArgs e)
        {
            Player.Name = ((Textbox)obj).Text;
            Properties.Settings.Default.LocalPlayerName = ((Textbox)obj).Text;
            Properties.Settings.Default.Save();
        }

        private void HideForm1(object obj, EventArgs e)
        {
            formCollection["form1"].Visible = false;
        }

        //private void ButtonGroup1_ChangeSelection(object obj, EventArgs e)
        //{
        //    string selection = obj as string;
        //    System.Diagnostics.Debug.WriteLine("ButtonGroup1.OnChangeSelection : " + selection);
        //}

        //private void ButtonRow1_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Selected Index : " + (formCollection["form1"]["buttonRow1"] as ButtonRow).SelectedIndex);
        //    System.Diagnostics.Debug.WriteLine("Selected Item  : " + formCollection["form1"]["buttonRow1"].Text);
        //}

        //private void Checkbox1_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Checkbox Value: " + ((Checkbox)obj).Value);
        //}

        //private void CheckboxGroup1_ChangeSelection(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("CheckboxGroup1.OnChangeSelection : " + (string)obj);
        //}

        //private void Form1_mnuFileClose(object obj, EventArgs e)
        //{
        //    formCollection["form1"].Close();
        //}

        //private void Form1_mnuFileExit(object obj, EventArgs e)
        //{
        //    if (graphics.IsFullScreen)
        //        graphics.ToggleFullScreen();

        //    this.Exit();
        //}

        //private void Form1_mnuViewToggleFS(object obj, EventArgs e)
        //{
        //    graphics.PreferredBackBufferWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
        //    graphics.PreferredBackBufferHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
        //    graphics.ToggleFullScreen();
        //}

        //private void Label1_MouseOut(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Label.OnMouseOut");
        //}

        //private void Label1_MouseOver(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Label.OnMouseOver");
        //}

        //private void Label1_OnPress(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Label.OnPress");
        //}

        //private void Label1_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("Label.OnRelease");
        //}

        //private void Listview_ColumnHeader0_OnPress(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("ListView ColumnHeader0 Pressed");
        //}

        //private void Listview_ColumnHeader0_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("ListView ColumnHeader0 Released");
        //}

        //private void msgbox0_OnOk(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox0 OK");
        //    System.Diagnostics.Debug.WriteLine("MessageBox0 OK");
        //}

        //private void msgbox1_OnCancel(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox1 Cancel");
        //    System.Diagnostics.Debug.WriteLine("MessageBox1 Cancel");
        //}

        //private void msgbox1_OnOk(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox1 OK");
        //    System.Diagnostics.Debug.WriteLine("MessageBox1 OK");
        //}

        //private void msgbox2_OnNo(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox2 No");
        //    System.Diagnostics.Debug.WriteLine("MessageBox2 No");
        //}

        //private void msgbox2_OnYes(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox2 Yes");
        //    System.Diagnostics.Debug.WriteLine("MessageBox2 Yes");
        //}

        //private void msgbox3_OnCancel(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox3 Cancel");
        //    System.Diagnostics.Debug.WriteLine("MessageBox3 Cancel");
        //}

        //private void msgbox3_OnNo(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox3 No");
        //    System.Diagnostics.Debug.WriteLine("MessageBox3 No");
        //}

        //private void msgbox3_OnYes(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox3 Yes");
        //    System.Diagnostics.Debug.WriteLine("MessageBox3 Yes");
        //}

        //private void msgbox4_OnOk(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox4 OK");
        //    //frmDebug.WriteLine("MessageBox4 Output: " + (string)obj);
        //    System.Diagnostics.Debug.WriteLine("MessageBox4 OK, Output : " + (string)obj);
        //}

        //private void msgbox5_OnCancel(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox5 Cancel");
        //    //frmDebug.WriteLine("MessageBox5 Output: " + (string)obj);
        //    System.Diagnostics.Debug.WriteLine("MessageBox5 Cancel, Output: " + (string)obj);
        //}

        //private void msgbox5_OnOk(object obj, EventArgs e)
        //{
        //    //frmDebug.WriteLine("MessageBox5 OK");
        //    //frmDebug.WriteLine("MessageBox5 Output: " + (string)obj);
        //    System.Diagnostics.Debug.WriteLine("MessageBox5 OK, Output : " + (string)obj);
        //}

        //private void PictureBox1_MouseOut(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("PictureBox.OnMouseOut");
        //}

        //private void PictureBox1_MouseOver(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("PictureBox.OnMouseOver");
        //}

        //private void PictureBox1_OnPress(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("PictureBox.OnPress");
        //}

        //private void PictureBox1_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("PictureBox.OnRelease");
        //}

        //private void Potentiometer_OnChangeValue(object obj, EventArgs e)
        //{
        //    ((Progressbar)formCollection["form1"].Controls["progressbar1"]).Value = (int)obj;
        //}

        //private void Radiobutton1_OnRelease(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("RadioButton Value: " + ((RadioButton)obj).Value);
        //}

        //private void RadioButtonGroup1_ChangeSelection(object obj, EventArgs e)
        //{
        //    System.Diagnostics.Debug.WriteLine("RadioButtonGroup1.OnChangeSelection : " + (string)obj);
        //}

        //private void Remove_OnPress(object obj, EventArgs e)
        //{
        //    Listbox listbox1 = (Listbox)formCollection["form1"]["listbox1"];
        //    listbox1.RemoveAt(listbox1.SelectedIndex);
        //}
#endregion

        #endregion Methods
    }
}